﻿using System;
using System.Collections;
using System.Data;

namespace VitCode.Castle.Helper
{
    /// <summary>
    /// EmptyColumn价差对象，主要对DataTtable检查空列
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class EmptyColumnChecker
    {
        private IList m_ColumnIndexes = new ArrayList();
        private DataTableCheckerHelper m_DataTableCheckerHelper;

        /// <summary>
        /// 内置构造函数，必须指定这个Checker的ExcelHelper的
        /// </summary>
        /// <param name="excelHelper"></param>
        internal EmptyColumnChecker(DataTableCheckerHelper excelHelper)
        {
            m_DataTableCheckerHelper = excelHelper;
        }

        /// <summary>
        /// 行中是否拥有为空的数据
        /// </summary>
        /// <param name="row"></param>
        /// <param name="rowIndex">行</param>
        /// <param name="errorResult">错误ErrorResulut</param>
        /// <returns>如果row中有带有空列,那么返回true,并且columnNams.Length不为0,否则返回false</returns>
        public void CheckIt(DataRow row, int rowIndex, ErrorResult errorResult)
        {
            foreach (ColumnIndex columnIndex in m_ColumnIndexes)
            {
                EachCheck(columnIndex, row, rowIndex, errorResult);
            }
        }

        /// <summary>
        /// Checker所属的ExcelHelper
        /// </summary>
        public DataTableCheckerHelper DataTableCheckerHelper
        {
            get { return m_DataTableCheckerHelper; }
        }

        /// <summary>
        /// 获取ColumnIndex
        /// </summary>
        public ColumnIndex this[int index]
        {
            get { return (ColumnIndex) this.m_ColumnIndexes[index]; }
        }

        /// <summary>
        /// 根据列名称增加需要被检查的列。如果输入为多个列,那么只有当
        /// 全部为空的时候才会被认为空
        /// </summary>
        /// <param name="args"></param>
        public virtual void AddColumnByName(params string[] args)
        {
            ColumnIndex cc = new ColumnIndex();
            cc.SetColumnName(args);
            m_ColumnIndexes.Add(cc);
        }

        /// <summary>
        /// 根据列索引增加需要被检查的列。如果输入为多个列,那么只有当
        /// 全部为空的时候才会被认为空
        /// </summary>
        /// <param name="indexes"></param>
        public virtual void AddColumnByIndex(params int[] indexes)
        {
            ColumnIndex cc = new ColumnIndex();
            cc.SetColumnIndex(indexes);
            m_ColumnIndexes.Add(cc);
        }

        /// <summary>
        /// 单项检查使用columnIndex检查行数据，并且把符合资格的行名称放入columnNamesCollection
        /// </summary>
        /// <param name="columnIndex">ColumnIndex对象</param>
        /// <param name="row">行对象</param>
        /// <param name="errorResult">错误结果集合</param>
        /// <param name="rowIndex">行在DAtaTable中的位置</param>
        protected virtual void EachCheck(ColumnIndex columnIndex, DataRow row, int rowIndex, ErrorResult errorResult)
        {
            int[] indexes = columnIndex.GetColumnIndexes(row.Table.Columns);

            int valueLength = 0;
            string caption = columnIndex.GetCaptionNames(row);
            for (int i = 0; i < indexes.Length; i++)
            {
                int dataColumnIndex = indexes[i];
                valueLength += Convert.ToString(row[dataColumnIndex]).Trim().Length;

            }

            if (valueLength == 0)
            {
                if (indexes.Length == 1)
                    errorResult.SetMessage(this.DataTableCheckerHelper.GetSourceDataIndex(rowIndex), "<font color=red>列名为" + caption + "不能为空</font>");
                else
                    errorResult.SetMessage(this.DataTableCheckerHelper.GetSourceDataIndex(rowIndex), "列名为" + caption + "不能同时为空");
            }
        }
    }
}
